package hangzhou;

import java.util.ArrayList;


import java.util.Iterator;
import java.util.List;



import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import hangzhou.denglu;

public class userDAO {
	private denglu dl;
    List<denglu> list=new ArrayList<>();
	public List<denglu> getAllUsers() {
		boolean res=false;
		String hql="from denglu";
		Session session=HibernateSessionFactory.getSession();
		Query query=session.createQuery(hql);
	    list=query.list();
	    session.close();
		return list;
	}
	public List<denglu> del(int id) {
		boolean res=false;
		Session session=HibernateSessionFactory.getSession();
		Transaction tran=session.beginTransaction();
		session.delete(selectById(id));
		tran.commit();
	    session.close();
		return getAllUsers();
	}
	public denglu selectById(int id) {
		boolean res=false;
		Session session=HibernateSessionFactory.getSession();
		Transaction tran=session.beginTransaction();
		dl=session.get(denglu.class, id);
		System.out.println(dl.getUsername());
		tran.commit();
	    session.close();
		return dl;
	}
	public List<denglu> update(int id,String upName,String upPWD){
		boolean res = false;
		Session session=HibernateSessionFactory.getSession();
		Transaction tran = session.beginTransaction();
		denglu dl=selectById(id);
		dl.setUsername(upName);
		dl.setPassword(upPWD);
		session.update(dl);
		tran.commit();
		session.close();
		return getAllUsers();
	}
	//获取当前页面的所有数据并放在list集合中。
		public List<denglu> findUserByPage(int currPage, int pageSize) {
			// TODO Auto-generated method stub
			 Session session=HibernateSessionFactory.getSession();
			 Query<denglu> query=session.createQuery("from denglu");
			 query.setFirstResult((currPage-1)*pageSize);
			 query.setMaxResults(pageSize);
			 List<denglu> list=query.list();
			 session.close();
			return list;
		}
		//获取数据库中总条数

		public int getCount() {
			Session session=HibernateSessionFactory.getSession();
			String hql="select count(u) from denglu u";
			//�˴��Ļ�ȡ�Ķ���ֵΪobject��ʵ��ΪLong���ͣ�ע����long����Ȼ����.intValue()תΪint��
			long total=(long) session.createQuery(hql).uniqueResult();
			return (int)total;
		}
		

	
}
